﻿using System;
using System.Data;
using System.Configuration;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.IO;

/// <summary>
/// Common 的摘要描述
/// </summary>
public class Common
{
    public Common()
	{
		//
		// TODO: 在此加入建構函式的程式碼
		//
	}

    /// <summary>
    /// 介面
    /// </summary>
    public class UI : System.Web.UI.Page
    {
        #region 顯示訊息方塊
        public static void ShowMessageBox(System.Web.UI.Page page, string message)
        {
            message = message.Replace("'", "\'").Replace("'", "\\'").Replace("\"", "\\\"").Replace("\r\n", "\\n").Replace("\r", "").Replace("\n", "\\n");
            System.Web.UI.ScriptManager.RegisterStartupScript(page, page.GetType(), "Message", "alert('" + message + "')", true);
        }
        #endregion
    }

    /// <summary>
    /// 取得市場慣例的幣別組資料
    /// </summary>
    /// <param name="currency1"></param>
    /// <param name="currency2"></param>
    /// <returns></returns>
    public static string getCurrencyPair(string currency1, string currency2)
    {
        string currencyPair = "";
        XElement element = null;

        #region 載入 CurrencyPair.xml
        string xmlPath = HttpContext.Current.Request.MapPath("~/App_Data/");
        if (File.Exists(xmlPath + @"CurrencyPair.xml"))
        {
            element = XElement.Load(xmlPath + @"CurrencyPair.xml");
        }
        else
        {
            element = new XElement("ItemList");
        }
        #endregion
        #region 取得對應之幣別組資料
        try
        {
            var data = element.Elements().Where(P => P.Element("Currency1").Value.ToUpper().Equals(currency1) && P.Element("Currency2").Value.ToUpper().Equals(currency2)).Single();
            if (data.Elements().Count() > 0)
                currencyPair = data.Element("Memo").Value;
            else
                currencyPair = "";
        }
        catch (Exception)
        {
            currencyPair = "";
            //throw;
        }
        #endregion

        return currencyPair;
    }

    /// <summary>
    /// 取得對應的標的資產明細代號資料
    /// </summary>
    /// <param name="ccy"></param>
    /// <param name="index"></param>
    /// <returns></returns>
    public static string getAssetCodeMaping(string ccy, string index)
    {
        string val = "OTHERS";
        XElement element = null;
        #region 載入 AssetCodeMaping.xml
        string xmlPath = HttpContext.Current.Request.MapPath("~/App_Data/");
        if (File.Exists(xmlPath + @"AssetCodeMaping.xml"))
        {
            element = XElement.Load(xmlPath + @"AssetCodeMaping.xml");
        }
        else
        {
            element = new XElement("ItemList");
        }
        #endregion
        #region 取得對應的標的資產明細代號資料
        try
        {
            var xmlData = element.Elements().Where(P => P.Element("CCY").Value.ToUpper().Equals(ccy) && P.Element("INDEX").Value.ToUpper().Equals(index)).Single();
            if (xmlData.Elements().Count() > 0)
                val = xmlData.Element("MAPING").Value;
        }
        catch (Exception)
        {
        }
        #endregion

        return val;
    }
    /// <summary>
    /// 依Summit Basis 取得對應OTC TR系統的計息天數類別
    /// Actual/365
    /// Actual/Actual
    /// Actual/365(Fixed)
    /// Actual/360
    /// 30/360
    /// 360/360
    /// Bond Basis
    /// 其他
    /// </summary>
    /// <param name="basis"></param>
    /// <returns></returns>
    public static char getBasisCode(string basis)
    {
        char rtn = 'Z';
        switch (basis)
        {
            case "D365":
                rtn = '1';
                break;
            case "ACT":
                rtn = '2';
                break;
            case "A365F":
                rtn = '3';
                break;
            case "A360":
                rtn = '4';
                break;
            case "30/360":
                rtn = '5';
                break;
            case "DRV":
                rtn = '6';
                break;
            case "EURBD":
                rtn = '7';
                break;
            default:
                rtn = 'Z';
                break;
        }

        return rtn;
    }
    /// <summary>
    /// 將CNH幣別更改為CNY
    /// </summary>
    /// <param name="ccy"></param>
    /// <returns></returns>
    public static string replaceCNHToCNY(string ccy)
    {
        string rtn = ccy;
        if (rtn.ToUpper().Equals("CNH"))
            rtn = "CNY";

        return rtn;
    }
}
